package src.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class CountUtil {
    final static String CHARSET_NAME = "UTF-8";


    public static String Characters(String path) {
        String result;
        int sum = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), CHARSET_NAME));
            String line;
            while ((line = in.readLine()) != null) {
                for (int i = 0; i < line.length(); i++) {
                    char c = line.charAt(i);
                    if (c <= 'z' && c >= 'a') sum++;
                }
            }
            result = "字符数为：" + sum;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static String Words(String path) {
        String result;
        int sum = 0;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), CHARSET_NAME));
            String line;
            while ((line = in.readLine()) != null) {
                String[] split = line.split("[\\s+,.\n]");
                sum += split.length;
            }
            result = "单词数为：" + sum;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
